package org.example;

import com.alibaba.fastjson.JSON;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.CheckedOutputStream;

/**
 * @author liukaihao
 * @date 2025/03/09
 */
public class Day14 {
    public static void main(String[] args) {
        System.out.println("hello word");
        // 给定一个字符串数组，编写一个程序，找出所有长度大于等于5的字符串，并按字母顺序排序
        String[] s1 = {"adefsda","wetres","qweqw","desfsd"};
        List<String> collect = Arrays.stream(s1).filter(o -> o.length() >= 5).sorted().collect(Collectors.toList());
        System.out.println("第一题："+collect);
        // 2. 给定一个整数数组，编写一个程序，计算数组中所有的偶数的平均数
        int[] sry2 = {1,2,3,4,5,6,9};
        double v = Arrays.stream(sry2).filter(n -> n % 2 == 0).average().orElse(0.0);
        System.out.println("第二题："+v);
        // 3. 给定一个字符串数组，编写一个程序，计算所有字符串‘a’出现的次数，
        String[] sry3 = {"sfsfgasfsa","afdsfsfaa","fdsfdfw","fafasfa"};
        long count = Arrays.stream(sry3).flatMapToInt(CharSequence::chars).filter(c -> c == 'a').count();
        System.out.println("第三题："+ count);
        // 4. 给定一个整形数组，编写一个程序，去除重复的元素，并按升序排序
        int[] sry4 = {6,4,2,8,3,5,4};
        int[] ints = Arrays.stream(sry4).distinct().sorted().toArray();
        System.out.println("第四题："+Arrays.toString(ints));
        // 5. 给定一个用户列表，编写一个程序，筛选出年龄大于等于18岁且名字长度大于等于5的用户，并按照名字长度降序排序
        List<User2> list = Arrays.asList(
            new User2("zhangsan",18),
                new User2("lisi",45),
                new User2("wangwu",75),
                new User2("zhaoliu",46),
                new User2("tianqi",43)
        );
        List<User2> collect1 = list.stream().filter(user -> user.getAge() >= 18).filter(user -> user.getName().length() >= 5)
                .sorted(Comparator.comparingInt((User2 u) -> u.getName().length()).reversed())
                .collect(Collectors.toList());
        System.out.println("第五题：");
        collect1.forEach(System.out::println);
        System.out.println(JSON.toJSONString(collect1));

        int[] sry6 = {6,3,5,7,9,50};
        int sum = Arrays.stream(sry6).sum();
        System.out.println("第6题："+sum);
        String[] sry7 = {"fsafs","fweeffw","fsfsa"};
        String collect2 = Arrays.stream(sry7).collect(Collectors.joining());
        System.out.println(collect2);

        int[] sry8 = {6,5,3,2,1,8,7,9};
        int sum1 = Arrays.stream(sry8).map(x -> x * x).sum();
        System.out.println("第8题："+sum1);



    }
}
